$(document).ready(function(){
  //提交时统一进行表单验证
  $("#register1").click(function(){ 
    //表单验证
    if(company_flag==0){
      return false;
    }
    if($("#com-name").val()==""){
      $("#com-named").css("display","inline");
      return false;
    }else{
      $("#com-named").css("display","none");
    }
    if($("#com-shortname").val()==""){
      $("#com-shortnamed").html("*请输入企业简称");
      $("#com-shortnamed").css("display","inline");
      return false;
    }else{
      $("#com-shortnamed").css("display","none");
    }
    if($("#contact-address").val()==""){
      $("#contact-addressd").css("display","inline");
      return false;
    }else{
      $("#contact-addressd").css("display","none");
    }
    if($("#legal-person").val()==""){
      $("#legal-persond").css("display","inline");
      return false;
    }else{
      $("#legal-persond").css("display","none");
    }
    // if($("#business_type").html()=="请选择"){
    //   $("#comwrapd").css("display","inline");
    //   return false;
    // }else{
    //   $("#comwrapd").css("display","none");
    // }
    if($("#logo-image").attr("src")==""){
      $("#reg-uploadd").html("*请上传企业logo");
      $("#reg-uploadd").css("margin-left","30px")
      return false;
    }else{
      $("#reg-uploadd").html("");
    }
    if($("#licence-image").attr("src")==""){
      $("#reg-uploadd").html("*请上传营业执照");
      $("#reg-uploadd").css("margin-left","300px")
      return false;
    }else{
      $("#reg-uploadd").html("");
    }
    if($("#contact-name").val()==""){
      $("#contact-named").css("display","inline");
      return false;
    }else{
      $("#contact-named").css("display","none");
    }
    if($("#contact-position").val()==""){
      $("#contact-positiond").css("display","inline");
      return false;
    }else{
      $("#contact-positiond").css("display","none");
    }
    if($("#contact-mobile").val()==""){
      $("#contact-mobiled").css("display","inline");
      $("#contact-mobiled").html("*请输入手机号码");
      return false;
    }else{
      $("#contact-mobiled").css("display","none");
    }
    var mobile_regcheck=/^1[34578]\d{9}$/;
    if(mobile_regcheck.test($("#contact-mobile").val())){
      $("#contact-mobiled").css("display","none");
    }else{
      $("#contact-mobiled").css("display","inline");
      $("#contact-mobiled").html("*请输入正确的号码");
      return false;
    }
    if($("#contact-phone").val()==""){
      $("#contact-phoned").css("display","inline");
      $("#contact-phoned").html("*请输入固定电话");
      return false;
    }else{
      $("#contact-phoned").css("display","none");
    }
    var phone_regcheck=/^0\d{2,3}-\d{7,8}$/;
    if(phone_regcheck.test($("#contact-phone").val())){
      $("#contact-phoned").css("display","none");
    }else{
      $("#contact-phoned").css("display","inline");
      $("#contact-phoned").html('*格式为"区号+固定电话"');
      return false;
    }
    if($("#contact-email").val()==""){
      $("#contact-emaild").css("display","inline");
      $("#contact-emaild").html('*请输入E-mail');
      return false;
    }else{
      $("#contact-emaild").css("display","none");
    }
    var email_regcheck=/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
    if(email_regcheck.test($("#contact-email").val())){
      $("#contact-emaild").css("display","none");
    }else{
      $("#contact-emaild").css("display","inline");
      $("#contact-emaild").html('*请输入正确的E-mail');
      return false;
    }
  //   fullname,         //企业全称
  // website,          //网址
  // corporate,        //法人
  // business_field,   //业务类型
  // contact_name,     //联系人名
  // contact_position, //联系人职位
  // contact_mobile,   //联系人电话
  // contact_tax,      //固定电话
  // contact_email,    //联系人email
  // address,          //地址
  // code 
    
    //数据传递
    var post_name=$("#com-shortname").val();
    var post_fullname=$("#com-name").val();
    var post_website=$("#website").val();
    var post_corporate=$("#legal-person").val();
    var post_business_field=$("#selected_business_type").val();
    var post_contact_name=$("#contact-name").val();
    var post_contact_position=$("#contact-position").val();
    var post_contact_mobile=$("#contact-mobile").val();
    var post_contact_tax=$("#contact-phone").val();
    var post_contact_email=$("#contact-email").val();
    var post_address=$("#contact-address").val();
    var post_code=$("#yanzhengma").val();
    //console.log($('#licence-image').attr("src"));
    var post_logo_str=$('#logo-image').attr("src");
    var post_license_str=$('#licence-image').attr("src");
    post_reg_company(
                  post_name,
                  post_fullname,
                  post_website,
                  post_corporate,
                  post_business_field,
                  post_contact_name,
                  post_contact_position,
                  post_contact_mobile,
                  post_contact_tax,
                  post_contact_email,
                  post_address,
                  post_code
                  );
    post_company_pics(post_name, post_logo_str, post_license_str);
    // var data = {"uname":username,"upwd":password};
    // $.ajax({ 
    //   url: '/register',
    //   type: 'post',
    //   data: data,
    //   success: function(data,status){ 
    //     if(status == 'success'){ 
    //       $("#reg-doing").css("display","none");
    //       $("#reg-suc").css("display","block");
    //       setTimeout(function(){
    //         location.href = 'register';
    //       },3000);
    //     }
    //   },
    //   error: function(data,err){ 
    //     //location.href = 'reg_company';
    //   }
    // });
  });
  //简称验证
  $("#com-shortname").blur(function(){
    get_company_name_check($("#com-shortname").val());
  });
  //验证码验证
  $("#yanzhengma").blur(function(){
    get_regcode_list($("#yanzhengma").val());
  });
  //上传图片
  $('#licence-img').click(function(){
    $('#licbtn').click();
  });
  $('#licbtn').change(function(){
    var file = this.files[0]; //选择上传的文件
    if(!/\.(gif|jpg|jpeg|png|gif|jpg|png)$/i.test(file.name)){
        alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
        return false;
    }
    if(file.size>1024*1024*2){
      alert("传输图片过大");
      return false;
    }
    var r = new FileReader();
    r.readAsDataURL(file); //Base64
    $(r).load(function(){
      console.log(this.result)
      $('#licence-img').css("border","0");
      $('#licence-img span').css("display","none");
      $('#licence-image').attr("src",this.result);
      $('#licence-image').css("display","block");
    });
  });
  $('#logo-img').click(function(){
    $('#licbtnlogo').click();
  });
  $('#licbtnlogo').change(function(){
    var file = this.files[0]; //选择上传的文件
    if(!/\.(gif|jpg|jpeg|png|gif|jpg|png)$/i.test(file.name)){
        alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
        return false;
    }
    // var logo_image=$('#logo-image').attr("offsetWidth");
    // console.log($('#logo-image'));
    // console.log($('#logo-image')[0].offsetWidth);
    
    if(file.size>300*300){
      alert("传输图片过大");
      return false;
    }
    var r = new FileReader();
    r.readAsDataURL(file); //Base64
    $(r).load(function(){
      $('#logo-img').css("border","0");
      $('#logo-img span').css("display","none");
      $('#logo-image').attr("src",this.result);
      $('#logo-image').css("display","block");
    });
  });

  //自定义下拉列表
  var types = document.getElementById('selected_business_type').getElementsByTagName('a');
  for(var i=0; i<types.length; i++){
    types[i].addEventListener('click', function() {
      document.getElementById('business_type').innerHTML = this.innerHTML;
    });
  }
});

function post_reg_company(
  name,             //企业名 简称
  fullname,         //企业全称
  website,          //网址
  corporate,        //法人
  business_field,   //业务类型
  contact_name,     //联系人名
  contact_position, //联系人职位
  contact_mobile,   //联系人电话
  contact_tax,      //固定电话
  contact_email,    //联系人email
  address,          //地址
  code              //验证码
){
  var data = {
    "name":name,
    "fullname":fullname,
    "website":website,
    "corporate":corporate,
    "business_field":business_field,
    "contact_name":contact_name,
    "contact_position":contact_position,
    "contact_mobile":contact_mobile,
    "contact_tax":contact_tax,
    "contact_email":contact_email,
    "address":address,
    "code":code
  };
  $.ajax({
    url: '/reg_company',
    type: 'post',
    data: data,
    success: function(data,status){
      if(status == 'success'){
        //location.href = 'login';
        console.log(data);
      }
    },
    error: function(data,err){
      //location.href = 'reg_company';
      console.log(data);
    }
  });
}
var company_flag=0;
function get_company_name_check(name){
  var company_info = {
    name : name
  };
  $.ajax({
    url: '/company_name_check',
    type: 'get',
    data: company_info,
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        if($("#com-shortname").val()==""){
          $("#com-shortnamed").html("*请输入企业简称");
          $("#com-shortnamed").css("display","inline");
          $("#com-shortnamed").css("color","red");
          company_flag=0;
        }else{
          if(data.exist==false){
            $("#com-shortnamed").html("*该公司简称可以使用");
            $("#com-shortnamed").css("display","inline");
            $("#com-shortnamed").css("color","green");
            company_flag=1;
          }else{
            $("#com-shortnamed").html("*该公司简称已被占用");
            $("#com-shortnamed").css("display","inline");
            $("#com-shortnamed").css("color","red");
            company_flag=0;
          }
        }
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function post_company_pics(name, logo_str, license_str){
  var company_info = {
    name : name,
    logo_str : logo_str,
    license_str : license_str
  };
  $.ajax({
    url: '/company_pics',
    type: 'post',
    data: company_info,
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function refresh_ccap()
{
  var xmlHTTP = new XMLHttpRequest();
  xmlHTTP.open('GET','/ccap',true);

  // Must include this line - specifies the response type we want
  xmlHTTP.responseType = 'arraybuffer';

  xmlHTTP.onload = function(e)
  {

    var arr = new Uint8Array(this.response);


    // Convert the int array to a binary string
    // We have to use apply() as we are converting an *array*
    // and String.fromCharCode() takes one or more single values, not
    // an array.
    var raw = String.fromCharCode.apply(null,arr);

    // This works!!!
    var b64=btoa(raw);
    var dataURL="data:image/jpeg;base64,"+b64;
    document.getElementById("yam-image").src = dataURL;
  };

  xmlHTTP.send();
}

function get_ccap_check(code){
  var company_info = {
    code : code
  }
  $.ajax({
    url: '/ccap_check',
    type: 'get',
    data: company_info,
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function checkImgPX(ths, width, height) {  
    var img = null;  
    img = document.createElement("img");  
    document.body.insertAdjacentElement("beforeEnd", img); // firefox不行  
    img.style.visibility = "hidden";   
    console.log(ths[0].value);
    img.src = ths[0].value;  
    var imgwidth = img.offsetWidth;  
    var imgheight = img.offsetHeight;  
    console.log(imgwidth);
    alert(imgwidth + "," + imgheight);  
       
    if(imgwidth > width || imgheight > height) {  
        alert("图的最大尺寸应该是" + width + "x"+ height);  
        ths.value = "";  
        return false;  
    }  
    return true;  
} 